package zhoufu.sz.sanxiao_check.presents;

import android.text.TextUtils;
import android.util.SparseArray;
import java.util.List;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.models.BuildStreetInfo;
import zhoufu.sz.sanxiao_check.models.ReseauModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.models.UpdateStreetInfoModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem1Contract;
import zhoufu.sz.sanxiao_check.utils.SharedPreferencesUtil;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/20
 */
public class BasicMsgItem1Presenter implements BasicMsgItem1Contract.Presenter
{
    private BasicRequestRepository _requestHelper;

    private List<ReseauModel> mList;

    private BasicMsgItem1Contract.View view;

    private int mCurrentID;

    private SparseArray<List<ReseauModel>> array;
    private SharedPreferencesUtil          mPreferencesUtil;

    @Inject
    public BasicMsgItem1Presenter(BasicRequestRepository _requestHelper,
            SharedPreferencesUtil mPreferencesUtil)
    {
        this.mPreferencesUtil = mPreferencesUtil;
        this._requestHelper = _requestHelper;
        array = new SparseArray();
    }

    public void bindView(BasicMsgItem1Contract.View view)
    {
        this.view = view;
    }


    @Override public void onStart()
    {
        ReseauModel remodel = (ReseauModel) mPreferencesUtil
                .readEntity("GetGridList", ReseauModel.class);
        if ( remodel != null )
        {
            mList = remodel.getData();
            setGridListData();
        }
    }

    private void setGridListData()
    {
        if ( mList != null && mList.size() > 0 )
        {
            for (int i = 0; i < mList.size(); i++)
            {
                array.put(i, mList.get(i).getDistrictList());
            }
            if ( array.size() > 0 )
            {
                mCurrentID = 0;
                view.setRightAdapter(array.get(mCurrentID));
            }
            view.setLeftAdapter(mList);
        }
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        _requestHelper.onDestory();
    }


    @Override public void checkLeft(int position)
    {
        if ( mList == null )
        {
            return;
        }
        mCurrentID = position;
        if ( array.indexOfKey(mCurrentID) > -1 )
        {
            view.setRightAdapter(array.get(mCurrentID));
        }
    }


    @Override public void update(StreetInfoModel model)
    {
        BuildStreetInfo info = new BuildStreetInfo();
        info.setInfo(model);
        if ( zhoufu.sz.sanxiao_check.utils.TextUtils.isEmpty(info.getDistrict_ID() + "")
                || TextUtils.isEmpty(info.getName()) || TextUtils.isEmpty(info.getAddress())
                || TextUtils.isEmpty(info.getBusiness()) || TextUtils.isEmpty(info.getHas_Business())
                || TextUtils.isEmpty(info.getAcreage()) || TextUtils.isEmpty(info.getBuilding_No())
                || TextUtils.isEmpty(info.getRun_Name()) || TextUtils.isEmpty(info.getRun_Phone())
                || TextUtils.isEmpty(info.getDoorplate()) )
        {
            view.showToast("请完善所有信息再提交");
            return;
        }
        //if ( info.getPlace_x() < 2 || info.getPlace_y() < 2 )
        //{
        //    view.showToast("请点击定位图标获取位置信息");
        //    return;
        //}
        {
            _requestHelper.UpdateStreetInfo(info,
                    new RequestHelper.RequestCallBack<UpdateStreetInfoModel>()
                    {
                        @Override public void onPullStart()
                        {
                            view.pullStart();
                        }

                        @Override public void onPullFailud(Throwable throwable)
                        {
                            view.showToast(UIUtils.getString(R.string.toast_network_error));
                        }

                        @Override public void onPullEnd()
                        {
                            view.pullEnd();
                        }

                        @Override public void onPullSuccessed(UpdateStreetInfoModel s)
                        {
                            if ( s.isSuccess() )
                            {
                                view.showToast("数据已更新");
                                if ( s.getData().size() > 0 )
                                {
                                    view.updateSuccese(s.getData().get(0));
                                }
                            } else
                            {
                                view.showToast(s.getErrMsg());
                            }
                        }
                    });
        }
    }


    @Override
    public ReseauModel checkRight(int position)
    {
        if ( array.size() == 0 || array.get(mCurrentID).size() == 0 )
        {
            return new ReseauModel();
        }
        ReseauModel model = array.get(mCurrentID).get(position);
        model.setSuperID(mList.get(mCurrentID).getID());
        model.setSuperName(mList.get(mCurrentID).getName());
        return model;
    }
}
